@extends('admin.layouts.app')
@section('css')
<link href="{{asset('assets/plugins/switchery/css/switchery.min.css')}}" rel="stylesheet" />
@endsection
@section('content')
@include('vendor.ueditor.assets')

                        <!-- Page-Title -->
                        <div class="row">
                            <div class="col-sm-12">
                                <h4 class="page-title">平台管理</h4>
                                <ol class="breadcrumb">
                                    <li><a href="#">系统</a></li>
                                    <li><a href="{{url('zadmin/product')}}">商品管理</a></li>
                                    <li class="active">添加商品</li>
                                </ol>
                            </div>
                        </div>
                        
                        <div class="row">
                        	<div class="col-lg-12">
                        		<div class="card-box">
                <h2 class="m-t-0 header-title">商品规格设置</h2>
                            <hr/>
                             
                              @include('admin.common.error')
                        			<div class="row">
			                        	<div class="col-md-12">
                                  <form class="form-horizontal" role="form" action="{{url('zadmin/products/spec-save')}}" method="post">

                                              <div class="form-group">
                                                  <label class="col-md-2 control-label">商品名</label>
                                                  <div class="col-md-10">
                    <input type="text" class="form-control" name="name" required="" value="{{$product->name}}" disabled="">
                                                  </div>
                                              </div>
                                             
    

           
 @foreach($specs as $spec)             
<div class="form-group">
           
            <label class="col-md-2 control-label">{{$spec->name}}</label>
            <div class="col-md-10">
            
           
              <table class="table spec-table-{{$spec->id}}">
              <tr>  
                <th>属性</th>
               
                <th>图片</th>
                <th>操作</th>
              </tr> 
              @forelse($productSpecs as $ps)
              @if($ps->spec_id==$spec->id)
              <tr class="spec-tr-{{$spec->id}}">
              <td>

               <input class="form-control" type="hidden" value="{{$spec->id}}" name="spec_id[]"  />
                <input class="form-control" type="text" value="{{$ps->spec_value}}" name="spec_value[]" required="" />
              </td>
             
              <td>
                <input class="form-control" type="hidden" value="{{$ps->spec_thumb}}" name="spec_thumb[]" />
                <button class="btn btn-sm" type="button" name="upload_thumb">选择图片</button>
                <img src="{{$ps->spec_thumb}}" width="45" />
              </td>
             
              <td>
                <button class="btn btn-info btn-sm spec-add" data-specid="{{$spec->id}}" type="button">添加</button>
                <button class="btn btn-danger btn-sm spec-del" data-specid="{{$spec->id}}" type="button">删除</button>
              </td>
              </tr>
              @endif
              @empty


              <tr class="spec-tr-{{$spec->id}}">
              <td>

               <input class="form-control" type="hidden" value="{{$spec->id}}" name="spec_id[]"  />
                <input class="form-control" type="text" value="" name="spec_value[]" required="" />
              </td>
             
              <td>
                <input class="form-control" type="hidden" value="" name="spec_thumb[]" />
                <button class="btn btn-sm" type="button" name="upload_thumb">选择图片</button>
                <img src="" width="45" />
              </td>
             
              <td>
                <button class="btn btn-info btn-sm spec-add" data-specid="{{$spec->id}}" type="button">添加</button>
                <button class="btn btn-danger btn-sm spec-del" data-specid="{{$spec->id}}" type="button">删除</button>
              </td>
              </tr> 
              @endforelse
            </table> 

            </div>
  </div>
 @endforeach

 
                                              

                                              

                                              <div class="form-group text-center">
                                                  
                                                  <button type="submit" class="btn btn-info waves-effect waves-light">保存</button>
                                              </div>
                                              
                                              
                             {{csrf_field()}}
                    <input   name="product_id" type="hidden" value="{{$product->id}}">         
                    </form>
                                </div>
			                        	
			                        </div>

                             
			                        
                        			
                        		</div>
                                
                            </div> <!-- end col -->

                            
                        </div>
@endsection
                        

    
  @section('js')
  <script type="text/javascript" src="/js/plupload.full.min.js"></script>
  <script type="text/javascript" src="{{asset('assets/plugins/switchery/js/switchery.min.js')}}"></script>

        <script type="text/javascript">
         
          $(document).on('click','[name="upload_thumb"]', function() {
          var _this = $(this);  
          var uploader = new plupload.Uploader({
              runtimes : 'html5,flash,silverlight,html4',
              browse_button : this,               
              url : '/upload.php',
              flash_swf_url : '../js/Moxie.swf',
              silverlight_xap_url : '../js/Moxie.xap',
              
              filters : {
                max_file_size : '10mb',
                mime_types: [
                  {title : "Image files", extensions : "jpg,gif,png"},
                  {title : "Zip files", extensions : "zip"}
                ]
              },

              init: {
                PostInit: function() {
                  $('#filelist').html('');

                  
                },

                FilesAdded: function(up, files) {
                  plupload.each(files, function(file) {
                    
                     uploader.start();
                     return false;
                  });
                },

               

                 FileUploaded: function(up, file, info) {
                    // Called when file has finished uploading
                    var rs =  JSON.parse(info.response);
                    if(rs.status){
                      _this.siblings("img").attr('src',rs.data);
                      _this.siblings('input').val(rs.data);
                    }
                    console.info(rs);                              
                },
              }
          });
          uploader.init();
        });  


      

       $(document).on('click','.spec-add',function(){
        var specid = $(this).data('specid');
         var _tr   = $('.spec-tr-'+specid+':first').clone();
         _tr.find('input[name="spec_value[]"]').val('');
         $('.spec-table-'+specid).append(_tr);
       })

       $(document).on('click','.spec-del',function(){
         var _length = $('.spec-tr-'+specid).length;
         if(_length==1) return ;
         console.info(_length);
         $(this).parents('tr').remove();
       })
        </script>
        
        @endsection
    
